package com.dszq.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dszq.apis.wemedia.IWemediaClient;
import com.dszq.common.constants.UserConstants;
import com.dszq.model.common.dtos.PageResponseResult;
import com.dszq.model.common.dtos.ResponseResult;
import com.dszq.model.common.enums.AppHttpCodeEnum;
import com.dszq.model.user.dtos.AuthDto;
import com.dszq.model.user.pojos.ApUser;
import com.dszq.model.user.pojos.ApUserRealName;
import com.dszq.model.wemedia.pojos.WmUser;
import com.dszq.user.mapper.ApUserMapper;
import com.dszq.user.mapper.ApUserRealNameMapper;
import com.dszq.user.service.ApUserRealNameService;
import com.dszq.user.service.ApUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Lenovo]
 * @version : [v1.0]
 * @createTime : [2024/4/30 17:50]
 */
@Service
public class ApUserRealNameImpl extends ServiceImpl<ApUserRealNameMapper, ApUserRealName> implements ApUserRealNameService {
    /**
     * 提交请求获取作者列表。
     *
     * @param authDto 包含授权信息的数据传输对象，用于请求认证和权限验证。
     * @return 返回一个响应结果，其中包含作者列表或其他相关信息。
     */
    @Override
    public ResponseResult listAuthor(AuthDto authDto) {
        if (authDto==null)
        {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        authDto.checkParam();

        IPage page = new Page(authDto.getPage(),authDto.getSize());
        LambdaQueryWrapper<ApUserRealName> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (authDto.getStatus()!=null){
            lambdaQueryWrapper.eq(ApUserRealName::getStatus,authDto.getStatus());
        }
        page = page(page,lambdaQueryWrapper);
        ResponseResult responseResult = new PageResponseResult(authDto.getPage(),authDto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * @param authDto
     * @param status
     * @return
     */
    @Override
    public ResponseResult updateStatus(AuthDto authDto, Short status) {
        if (authDto==null||authDto.getId()==null)
        {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserRealName apUserRealName = new ApUserRealName();
        apUserRealName.setId(authDto.getId());
        apUserRealName.setStatus(authDto.getStatus());
        if (StringUtils.isNotBlank(authDto.getMsg()))
        {
            apUserRealName.setReason(authDto.getMsg());
        }
        updateById(apUserRealName);
        if (status.equals(UserConstants.OK_CHECK))
        {
            ResponseResult responseResult = createWmUserAndAuthor(authDto);
            return responseResult;
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private IWemediaClient wemediaClient;
    private ResponseResult createWmUserAndAuthor(AuthDto authDto) {
        Integer authDtoId = authDto.getId();
        ApUserRealName apUserRealName = getById(authDtoId);
        if (apUserRealName==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        Integer userId = apUserRealName.getId();
        ApUser apUser = apUserMapper.selectById(userId);
        if(apUser == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        WmUser wmUser =  wemediaClient.selectUserByName(apUser.getName());
        if (wmUser == null){
            WmUser wmUser1 = new WmUser();
            wmUser1.setApUserId(apUser.getId());
            wmUser1.setCreatedTime(new Date());
            wmUser1.setName(apUser.getName());
            wmUser1.setPassword(apUser.getPassword());
            wmUser1.setSalt(apUser.getSalt());
            wmUser1.setPhone(apUser.getPhone());
            wmUser1.setStatus(9);
            wemediaClient.saveWmUSer(wmUser1);
        }
        apUser.setFlag((short) 1);
        apUserMapper.updateById(apUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }
}
